package com.hbm.usercenter.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hbm.usercenter.R;
import com.hbm.usercenter.fragment.LoginFragment;
import com.hbm.usercenter.fragment.PhLoginFragment;
import com.hbm.usercenter.fragment.RegisterFragment;
import com.hbm.usercenter.fragment.ResetFragment;
import com.hbm.usercenter.utils.UIUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class LogAndRegActivity extends AppCompatActivity {

    @Bind(R.id.fl)
    FrameLayout mFl;
    @Bind(R.id.tv_more)
    TextView mTvMore;

    private LoginFragment mLoginFragment;
    private PhLoginFragment mPhLoginFragment;
    private RegisterFragment mRegisterFragment;
    private ResetFragment mResetFragment;
    private FragmentTransaction mFt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_and_reg);
        ButterKnife.bind(this);
        setSelect(0);
    }

    @OnClick(R.id.tv_more)
    public void onViewClicked() {
        showItemsDialog();
    }

    private void showItemsDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        /**
         * 设置内容区域为单选列表项
         */
        final String[] dialogItems = UIUtils.getStringArr(R.array.dialog_items);
        builder.setItems(dialogItems, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                switch (i) {
                    case 0:
                        setSelect(0);
                        break;
                    case 1:
                        setSelect(1);
                        break;
                    case 2:
                        setSelect(2);
                        break;
                    case 3:
                        setSelect(3);
                        break;
                }
                Toast.makeText(getApplicationContext(), "You clicked " + dialogItems[i], Toast
                        .LENGTH_SHORT).show();
            }
        });

        builder.setCancelable(true);
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    public void setSelect(int select) {
        FragmentManager fm = getSupportFragmentManager();
        mFt = fm.beginTransaction();
        hideFragment();
        switch (select) {
            case 0:
                if (mLoginFragment == null) {
                    mLoginFragment = new LoginFragment();
                    mFt.add(R.id.fl, mLoginFragment);
                }
                mFt.show(mLoginFragment);
                break;
            case 1:
                if (mPhLoginFragment == null) {
                    mPhLoginFragment = new PhLoginFragment();
                    mFt.add(R.id.fl, mPhLoginFragment);
                }
                mFt.show(mPhLoginFragment);
                break;
            case 2:
                if (mResetFragment == null) {
                    mResetFragment = new ResetFragment();
                    mFt.add(R.id.fl, mResetFragment);
                }
                mFt.show(mResetFragment);
                break;
            case 3:
                if (mRegisterFragment == null) {
                    mRegisterFragment = new RegisterFragment();
                    mFt.add(R.id.fl, mRegisterFragment);
                }
                mFt.show(mRegisterFragment);
                break;


        }
        mFt.commit();
    }

    private void hideFragment() {
        if (mLoginFragment != null) {
            mFt.hide(mLoginFragment);
        }
        if (mPhLoginFragment != null) {
            mFt.hide(mPhLoginFragment);
        }
        if (mRegisterFragment != null) {
            mFt.hide(mRegisterFragment);
        }
        if (mResetFragment != null) {
            mFt.hide(mResetFragment);
        }
    }
}
